﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Tenebrous_Manor.Navigation
{
    class RoomNavigator
    {
        Room current_room;
        Room old_room;

        private const String FILE_PATH = "..\\..\\..\\Content\\Maps\\";

        public int next_x_index = 0;
        public int next_y_index = 0;

        //Currently Initialize set the room to FOYER_ONE
        //When save is implemented, needs to be changed.
        public void Initialize()
        {
            this.current_room = Room.MAIN_ENTRANCE;
            this.old_room = Room.FOYER;
            this.next_x_index = GameConstants.COL_COUNT / 2;
            this.next_y_index = GameConstants.ROW_COUNT - 2; //Place character in front of main entrance door.
        }

        //Load will load the room array for the txt file
        public void Load(ref int[,] array)
        {
            using (StreamReader reader = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) 
                + FILE_PATH + current_room + ".txt"))
            {
                for (int j = 0; j < GameConstants.COL_COUNT; j++)
                {
                    string[] temp_array = reader.ReadLine().Split(',');

                    for (int i = 0; i < GameConstants.ROW_COUNT; i++)
                    {
                        array[i, j] = Convert.ToInt32(temp_array[i]);
                    }
                }
            }
        }


        public void SwitchRooms(Facing facing, int x, int y)
        {
            switch (current_room)
            {
                case Room.MAIN_ENTRANCE:
                    current_room = Room.FOYER;
                    next_x_index = x;
                    next_y_index = GameConstants.ROW_COUNT - 2;
                    break;

                case Room.FOYER:
                    switch(facing)
                    {
                        case Facing.DOWN:
                            current_room = Room.MAIN_ENTRANCE;
                            next_x_index = x;
                            next_y_index = 1;
                            break;
                        case Facing.LEFT:
                            current_room = Room.JUNK_ROOM;
                            next_x_index = GameConstants.COL_COUNT - 2;
                            next_y_index = y;
                            break;
                        case Facing.RIGHT:
                            current_room = Room.KITCHEN;
                            next_x_index = 1;
                            next_y_index = y;
                            break;
                        case Facing.UP:
                            current_room = Room.UPPER_LANDING;
                            next_x_index = x;
                            next_y_index = 3;
                            break;
                    }
                    break;

                case Room.KITCHEN:
                    current_room = Room.FOYER;
                    next_x_index = GameConstants.COL_COUNT - 2;
                    next_y_index = y;
                    break;

                case Room.JUNK_ROOM:
                    if (facing.Equals(Facing.RIGHT))
                    {
                        current_room = Room.FOYER;
                        next_x_index = 1;
                        next_y_index = y;
                    }
                    else if (facing.Equals(Facing.LEFT))
                    {
                        if (GameBools.getGameBool(GameBools.GameBool.STUDY))
                        {
                            current_room = Room.STUDY;
                            next_x_index = GameConstants.COL_COUNT - 2;
                            next_y_index = y;
                        }
                    }
                    break;

                case Room.STUDY:
                    if (x == 8)
                    {
                        current_room = Room.JUNK_ROOM;
                        next_x_index = 1;
                        next_y_index = y;
                    }
                    else if (GameBools.getGameBool(GameBools.GameBool.DESK_MOVED))
                    {
                        current_room = Room.LOWER_LANDING;
                        next_x_index = 7;
                        next_y_index = y;
                    }
                    break;

                case Room.UPPER_LANDING:
                    switch(facing)
                    {
                        case Facing.DOWN:
                            current_room = Room.FOYER;
                            next_x_index = x;
                            next_y_index = 1;
                            break;
                        case Facing.LEFT:
                            if (GameBools.getGameBool(GameBools.GameBool.BEDROOM))
                            {
                                current_room = Room.BEDROOM;
                                next_x_index = GameConstants.COL_COUNT - 2;
                                next_y_index = y;
                            }
                            break;
                    }
                    break;

                case Room.BEDROOM:
                    current_room = Room.UPPER_LANDING;
                    next_x_index = 1;
                    next_y_index = y;
                    break;

                case Room.LOWER_LANDING:
                    if (facing == Facing.RIGHT)
                        if (x == 7)
                        {
                            current_room = Room.STUDY;
                            next_x_index = 4;
                            next_y_index = 5;
                        }
                        else if (x == 8)
                        {
                            current_room = Room.BASEMENT;
                            next_x_index = 1;
                            next_y_index = y;
                        }
                    break;

                case Room.BASEMENT:
                    switch (facing)
                    {
                        case Facing.LEFT:
                            current_room = Room.LOWER_LANDING;
                            next_x_index = GameConstants.COL_COUNT - 2;
                            next_y_index = y;
                            break;
                        case Facing.RIGHT:
                            if (GameBools.getGameBool(GameBools.GameBool.RESEARCH_LAB))
                            {
                                current_room = Room.RESEARCH_LAB;
                                next_x_index = 1;
                                next_y_index = y;
                            }
                            break;
                    }
                    break;

                case Room.RESEARCH_LAB:
                    current_room = Room.BASEMENT;
                    next_x_index = GameConstants.COL_COUNT - 2;
                    next_y_index = y;
                    break;
            }
        }

        public Room getCurrentRoom()
        {
            return current_room;
        }

        public Room getOldRoom()
        {
            return old_room;
        }

        public bool hasSwitched()
        {
            if (!current_room.Equals(old_room))
            {
                this.old_room = current_room;
                return true;
            }

            return false;
        }

        public int getX()
        {
            return next_x_index;
        }

        public int getY()
        {
            return next_y_index;
        }

        public void Draw(SpriteBatch spriteBatch, SpriteFont font)
        {
            spriteBatch.DrawString(font, current_room.ToString(), new Vector2(10, 10), Color.White);
        }

    }
}
